var userName = 'user' + Math.floor((Math.random()*1000)+1);

var socket =  io.connect('http://localhost:9092');

socket.on('connect', function() {
  output('<span class="connect-msg">Client has connected to the server!</span>');
});

socket.on('chatevent', function(data) {
  output('<span class="username-msg">' + data.userName + ':</span> ' + data.message);
});

socket.on('lightRequest',function(data){
  console.log("进入lightRequest方法");
  output('<span class="username-msg">' + data.version );
});

socket.on('disconnect', function() {
  output('<span class="disconnect-msg">The client has disconnected!</span>');
});

            function sendDisconnect() {
                    socket.disconnect();
            }

function sendMessage() {
  var message = $('#msg').val();
  $('#msg').val('');

  if(message === "open"){
    // protobuf.load("HardWare.proto",function (err,root) {
    //     if(err) throw err;
    //     var lightRequest = root.lookup("CommandEntity.LightRequest");
    //     var version = lightRequest.create({version:"10001"});
    //     var versionBuffer = lightRequest.encode(version).finish();
    //     var decode =  lightRequest.decode(versionBuffer)
    //     console.log(lightRequest);
    //     socket.emit('lightRequest',lightRequest);
    // });
    var jsonObject = {version: 1001,
              cid: "clubD90001212"};
    socket.emit('lightRequest', jsonObject);
  }


}

  function output(message) {
    var currentTime = "<span class='time'>" +  moment().format('HH:mm:ss.SSS') + "</span>";
    var element = $("<div>" + currentTime + " " + message + "</div>");
    $('#console').prepend(element);
  }

    $(document).keydown(function(e){
        if(e.keyCode == 13) {
            $('#send').click();
        }
    });
